package com.lu.project.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import com.lu.project.common.PageResponse;
import com.lu.project.exception.BusinessException;
import com.lu.project.exception.ErrorCode;
import com.lu.project.exception.ThrowUtils;
import com.lu.project.mapper.MemberElderMapper;
import com.lu.project.mapper.UserMapper;
import com.lu.project.model.dto.MemberElderDto.MemberElderDto;
import com.lu.project.model.entity.MemberElder;
import com.lu.project.model.entity.User;
import com.lu.project.model.vo.MemberElderVo;
import com.lu.project.service.MemberElderService;
import com.lu.project.service.UserService;
import com.lu.project.utils.UserThreadLocal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 客户老人关联 Service 实现类
 */
@Service
public class MemberElderServiceImpl implements MemberElderService {

    @Autowired
    private MemberElderMapper memberElderMapper;

    @Resource
    private UserMapper userMapper;

    /**
     * 添加客户老人关联
     * @param memberElder 客户老人关联实体
     */
    @Override
    public void add(MemberElderDto memberElder) {

        //ElderVo elder = elderService.selectByIdCard(memberElder.getIdCard(), memberElder.getName());
        ThrowUtils.throwIf(UserThreadLocal.getUserId()==null,ErrorCode.NOT_LOGIN_ERROR,"当前用户未登录，请登录");
        ThrowUtils.throwIf(memberElder.getElderId()==null,ErrorCode.PARAMS_ERROR,"未填写绑定老人ID");
        User user = userMapper.selectById(memberElder.getElderId());
        if (user==null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"老人信息不存在");
        }

        MemberElder memberElder1 = BeanUtil.toBean(memberElder, MemberElder.class);
        memberElder1.setMemberId(UserThreadLocal.getUserId());

        memberElder1.setElderId(memberElder.getElderId());
        QueryWrapper<MemberElder> memberElderQueryWrapper = new QueryWrapper<>();
        memberElderQueryWrapper.eq(ObjectUtil.isNotNull(UserThreadLocal.getUserId()),"member_id",UserThreadLocal.getUserId());
        memberElderQueryWrapper.eq(ObjectUtil.isNotNull(memberElder.getElderId()),"elder_id",memberElder.getElderId());

        Long l = memberElderMapper.selectCount(memberElderQueryWrapper);
        ThrowUtils.throwIf(l>0,ErrorCode.OPERATION_ERROR,"老人已经与当前登录用户绑定");
        if (memberElder.getPhone() == null){
            memberElder1.setPhone(user.getMobile());
        }
        try {
            memberElderMapper.add(memberElder1);
        }catch (Exception e) {
            throw new BusinessException(ErrorCode.OPERATION_ERROR,"绑定信息失败");
        }
    }

    /**
     * 更新客户老人关联
     * @param memberElder 客户老人关联实体
     */
    @Override
    public void update(MemberElderDto memberElder) {
        Long elderId = memberElder.getElderId();
        User user = userMapper.selectById(elderId);
        ThrowUtils.throwIf(user==null,ErrorCode.NOT_FOUND_ERROR,"绑定老人的数据不存在");
        MemberElder memberElder1 = BeanUtil.toBean(memberElder, MemberElder.class);
        memberElderMapper.update(memberElder1);
    }

    /**
     * 根据ID删除客户老人关联
     * @param id 客户老人关联ID
     */
    @Override
    public void deleteById(Long id) {
        memberElderMapper.deleteById(id);
    }

    /**
     * 根据ID获取客户老人关联
     * @param id 客户老人关联ID
     * @return 客户老人关联实体
     */
    @Override
    public MemberElderVo getById(Long id) {
        MemberElder byId = memberElderMapper.getById(id);
        return BeanUtil.toBean(byId, MemberElderVo.class);
    }

    /**
     * 根据客户ID获取客户老人关联列表
     * @param memberId 客户ID
     * @return 客户老人关联列表
     */
    @Override
    public List<MemberElderVo> listByMemberId(Long memberId) {
        List<MemberElderVo> memberElders = memberElderMapper.listByMemberId(memberId);
        return memberElders;
    }

    /**
     * 根据老人ID获取客户老人关联列表
     * @param elderId 老人ID
     * @return 客户老人关联列表
     */
    @Override
    public List<MemberElderVo> listByElderId(Long elderId) {
        List<MemberElder> memberElders = memberElderMapper.listByElderId(elderId);
        return BeanUtil.copyToList(memberElders, MemberElderVo.class);
    }

    /**
     * 分页查询客户老人关联列表
     * @param memberId 客户ID
     * @param elderId 老人ID
     * @param pageNum 当前页码
     * @param pageSize 每页显示数量
     * @return 分页结果
     */
    @Override
    public PageResponse<MemberElderVo> listByPage(Long memberId, Long elderId, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        if (ObjectUtil.isEmpty(memberId)) {
            memberId = UserThreadLocal.getUserId();
        }
        Page<List<MemberElder>> page = memberElderMapper.listByCondition(memberId, elderId);
        return PageResponse.of(page,  MemberElderVo.class);
    }

    @Override
    public List<MemberElderVo> my() {
        Long userId = UserThreadLocal.getUserId();
        return listByMemberId(userId);
    }

}

